/*
 * Copyright (C) 2007-2009  ENAC, Pascal Brisset, Antoine Drouin
 *
 * This file is part of paparazzi.
 *
 * paparazzi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * paparazzi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with paparazzi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * @file modules/nav/common_flight_plan.c
 * Common flight_plan functions shared between fixedwing and rotorcraft.
 */

#include "modules/nav/common_flight_plan.h"

#include "generated/flight_plan.h"

/** In s */
uint16_t stage_time, block_time;

uint8_t nav_stage, nav_block;

/** To save the current block/stage to enable return */
uint8_t last_block, last_stage;
uint8_t last_wp UNUSED;

static uint8_t private_nav_stage, private_nav_block;
static uint8_t private_last_block, private_last_stage;

void common_flight_plan_init(void) {
  private_nav_stage = 0;
  nav_stage = 0;
  private_nav_block = 0;
  nav_block = 0;
  private_last_stage = 0;
  last_stage = 0;
  private_last_block = 0;
  last_block = 0;
  stage_time = 0;
  block_time = 0;
}

void nav_init_block(void)
{
  if (private_nav_block >= NB_BLOCK) {
    private_nav_block = NB_BLOCK - 1;
    nav_block = private_nav_block;
  }
  private_nav_stage = 0;
  nav_stage = private_nav_stage;
  block_time = 0;
  InitStage();
  nav_on_enter_block(private_nav_block); // generated by flight plan
}

void nav_goto_block(uint8_t b)
{
  if (b != private_nav_block) {
    /* To avoid a loop in a the current block */
    private_last_block = private_nav_block;
    last_block = private_last_block;
    private_last_stage = private_nav_stage;
    last_stage = private_last_stage;
  }
  nav_on_exit_block(private_nav_block); // generated by flight plan
  private_nav_block = b;
  nav_block = private_nav_block;
  nav_init_block();
}

void nav_goto_next_block(void) {
  if (private_nav_block < NB_BLOCK - 1) {
    nav_goto_block(private_nav_block + 1);
  } else {
    nav_goto_block(private_nav_block);
  }
}

void nav_goto_next_stage(void) {
  private_nav_stage++;
  nav_stage = private_nav_stage;
  InitStage();
}

void nav_return(uint8_t reset) {
  nav_on_exit_block(private_nav_block); // generated by flight plan
  private_nav_block = private_last_block;
  nav_block = private_nav_block;
  if (reset == 1) {
    private_nav_stage = 0;
  } else {
    private_nav_stage = private_last_stage;
  }
  nav_stage = private_nav_stage;
  block_time = 0;
  InitStage();
  nav_on_enter_block(private_nav_block); // generated by flight plan
}

uint8_t get_nav_block() {
  return private_nav_block;
}
uint8_t get_nav_stage() {
  return private_nav_stage;
}
uint8_t get_last_block() {
  return private_last_block;
}
uint8_t get_last_stage() {
  return private_last_stage;
}

void set_nav_block(uint8_t b){
  if (b >= NB_BLOCK) {
    private_nav_block = NB_BLOCK - 1;
  }
  else {
    private_nav_block = b;
  }
  nav_block = private_nav_block;
}

void set_nav_stage(uint8_t s) {
  private_nav_stage = s;
  nav_stage = s;
}

